package com.itheima.reggie.controller;

import com.itheima.reggie.common.R;
import com.itheima.reggie.dto.DishDto;
import com.itheima.reggie.dto.SetmealDto;
import com.itheima.reggie.entity.Dish;
import com.itheima.reggie.entity.Page;
import com.itheima.reggie.service.SetmealService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.time.LocalDateTime;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/setmeal")
@Api(tags = "套餐相关接口")
public class SetmealController {
    @Autowired(required = false)
    private SetmealService setmealService;

    /**
     * 新增套餐
     *
     * @param setmealDto
     * @param session
     * @return
     */
    @PostMapping
    @ApiOperation("新增套餐接口")
    public R<String> save(@RequestBody SetmealDto setmealDto, HttpSession session) {
        log.info("套餐信息为:{}", setmealDto);
        Long empId = (Long) session.getAttribute("employee");
        //更新时间
        setmealDto.setCreateUser(empId);
        setmealDto.setUpdateUser(empId);
        setmealDto.setCreateTime(LocalDateTime.now());
        setmealDto.setUpdateTime(LocalDateTime.now());
        setmealService.saveWithDish(setmealDto);
        return R.success("保存成功");
    }

    /**
     * 套餐分页查询
     *
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @GetMapping("/page")
    @ApiOperation(value = "套餐分页查询接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page" ,value = "页码" ,required = true),
            @ApiImplicitParam(name = "pageSize" ,value = "每页记录数" ,required = true),
            @ApiImplicitParam(name = "name" ,value = "套餐名称" ,required = true)
    })
    public R<Page<SetmealDto>> page(Integer page, Integer pageSize, String name) {
        Page<SetmealDto> pageResult = setmealService.findByPage(page, pageSize, name);
        return R.success(pageResult);
    }

    /**
     * 作用: 根据id数组批量删除
     *
     * @param ids 要删除的套餐id数组
     * @return
     */
    @DeleteMapping
    @Transactional
    @ApiOperation(value = "删除套餐接口")
    public R<String> delete(@RequestParam List<Long> ids) {
        setmealService.deleteByIds(ids);
        return R.success("删除成功");
    }

    /**
     * 作用; 根据套餐id查找
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation(value = "根据套餐id查找")
    public R<SetmealDto> getById(@PathVariable("id") Long id) {
        SetmealDto setmealDto = setmealService.findById(id);
        return R.success(setmealDto);
    }

    /**
     * 作用;添加菜品套餐
     * @param setmealDto  页面传递过来套餐和菜品
     * @param session
     * @return
     */
    @PutMapping
    @ApiOperation(value = "添加菜品套餐")
    public R<String> update(@RequestBody SetmealDto setmealDto,HttpSession session){
        //获取当前登录者,设置当前的修改人
        Long empId=(Long)session.getAttribute("employee");
        setmealDto.setUpdateUser(empId);
        setmealService.updateWithDish(setmealDto);
        return R.success("修改菜品成功");
    }

    /**
     * 启/停售 (可批量)
     * @param ids
     * @param status
     * @return
     */
    @PostMapping("/status/{status}")
    @ApiOperation(value = "启/停售")
    public R<String> updateStatus(@RequestParam("ids") List<Long> ids, @PathVariable("status") Integer status){
      setmealService.updateStatus(ids,status);
      return R.success("修改成功");
    }
}
